﻿// Autore: Mattia Barrasso

using System.Linq;
using NUnit.Framework;
using RecipesManager.Data;
using TechTalk.SpecFlow;
using White.Core.UIItems;

namespace RecipesManager.Specs.Negozio
{
    [Binding, Scope(Feature = "RimuoviNegozio")]
    class RimuoviNegozio_Steps : NegoziBDD
    {
        Button _confirm;
        Button _cancel;
        Button _deleteShop;

        void GetConfirmDialogUi()
        {
            _confirm = MainWindow.Get<Button>("6");
            _cancel = MainWindow.Get<Button>("7");
        }

        [BeforeScenario]
        public new void SetUpScenario()
        {
            base.SetUpScenario();
        }

        [AfterScenario]
        public new void TearDownScenario()
        {
            base.TearDownScenario();
        }


        [Given(Regex = @"il negozio ([a-zA-Z]+) esiste")]
        public void DatoNegozioEsistente(string negozio)
        {
            Manager.Shops.Add(negozio);
            Assert.That(Manager.Shops.Contains(negozio));
        }

        [Given(Regex = @"l'ingrediente ([a-zA-Z]+) esiste")]
        public void DatiIngredienteEsistente(string ingrediente)
        {
            Manager.Ingredients.Add(ingrediente, MeasureType.Weight);
            Assert.That(Manager.Ingredients.Contains(ingrediente));
        }

        [Given(Regex = "il negozio ([a-zA-Z]+) fornisce l'ingrediente ([a-zA-Z]+)")]
        public void DatoNegozioFornitore(string negozio, string ingrediente)
        {
            Manager.Shops[negozio].IngredientRatings.Add(ingrediente, ShopRating.Good);
        }

        [Given(Regex = @"seleziono il negozio ([a-zA-Z]+)")]
        public void DatoSelezionoNegozio(string negozio)
        {
            NavigateToShops();
            GetShopsUi();
            _deleteShop = MainWindow.Get<Button>("bt_delete");

            Assert.That(Manager.Shops.Contains(negozio));
            Assert.That(Shops.Items.Any(item => item.Text.Equals(negozio)));
            Shops.Select(negozio);
        }

        [When(Regex = @"elimino il negozio")]
        public void QuandoElimino()
        {
            _deleteShop.Click();
        }

        [Then(Regex = @"appare un messaggio")]
        public void AlloraSuccesso()
        {
            Assert.That(MainWindow.ModalWindows().Count() == 1);
        }

        [When(Regex = "confermo")]
        public void QuandoConfermo()
        {
            GetConfirmDialogUi();
            _confirm.Click();
            Assert.That(!MainWindow.ModalWindows().Any());
        }

        [When(Regex = "annullo")]
        public void QuandoAnnullo()
        {
            GetConfirmDialogUi();
            _cancel.Click();
            Assert.That(!MainWindow.ModalWindows().Any());
        }

        [Then(Regex = "il negozio ([a-zA-Z]+) non esiste")]
        public void AlloraNegozioNonEsiste(string negozio)
        {
            Assert.That(!Shops.Items.Exists(item => item.Text.Equals(negozio)));
            BackToMainMenu.Click();
            Assert.That(!Manager.Shops.Contains(negozio));
        }

        [Then(Regex = "il negozio ([a-zA-Z]+) esiste")]
        public void AlloraNegozioEsiste(string negozio)
        {
            Assert.That(Shops.Items.Exists(item => item.Text.Equals(negozio)));
            BackToMainMenu.Click();
            Assert.That(Manager.Shops.Contains(negozio));
        }
    }
}
